package cn.fan.utils;

import cn.fan.config.EncryProperties;
import cn.fan.pojo.User;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Encryption {

    @Autowired
    private EncryProperties encryProperties;

    public String generatePassword(String password,String salt){
        String algorithmName=encryProperties.getAlgorithmName();
        int times = encryProperties.getTimes();

        String encodedPassword = new SimpleHash(algorithmName,password,salt,times).toString();
        return encodedPassword;
    }

//    public String generatePassword(String password){
//        String salt = new SecureRandomNumberGenerator().nextBytes().toString();
//        return generatePassword(password,salt);
//    }

    public boolean auth(String password,User user){
        String encodedPassword=generatePassword(password,user.getSalt());
        return encodedPassword.equals(user.getPassword());
    }

}
